﻿var HandleDom = {
    IdTriggerClick: function (controlId, callback) {
        $("#" + controlId).live("click", callback);
    },
    ClassTriggerClick: function (controlClass, callback) {
        $("." + controlClass).live("click", callback);
    },
    ClassTriggerDoubleClick: function (controlClass, callback) {
        $("." + controlClass).live("dblclick", callback);
    },
    IdTriggerBlur: function (controlId, callback) {
        $("#" + controlId).blur(callback);
    },
    IdTriggerChange: function (controlId, callback) {
        $("#" + controlId).change(callback);
    }
    
};

var HandleData = {
    GetDataGridSelected: function (dataGridId) {
        return $('#' + dataGridId).datagrid('getSelected');
    },
    GetTreeGridSelected: function (treeGridId) {
        return $('#' + treeGridId).treegrid('getSelected');
    },
    GetValueComboTree: function (comboTreeId) {
        return $('#' + comboTreeId).combotree('getValue');
    },
    GetValueComboBox: function (comboBoxId) {
        return $('#' + comboBoxId).combobox('getValue');
    },
    SetValueComboTree: function (comboTreeId, value) {
        $('#' + comboTreeId).combotree('setValue', value);
    },
    SetValueComboBox: function (comboBoxId, value) {
        $('#' + comboBoxId).combobox('setValue', value);
    },
    SetValueNumberSpinner: function (numberSpinnerId, value) {
        $('#' + numberSpinnerId).numberspinner('setValue', value);
    },
    IsNull: function (rowData, callback) {
        if (rowData) {
            if (callback != undefined) {
                callback();
            }
        } else {
            HandleTips.Alert('请先选中一条数据，再进行相关操作！');
            //$('#dlg').dialog('close');
        }
    },
    LoadForm: function (formId, row) {
        $('#' + formId).form('load', row);
    },
    ClearForm: function (formId) {
        $('#' + formId).form('clear');
    },
    ReloadDataGrid: function (dataGridId) {

        $('#' + dataGridId).datagrid('reload');
    },
    ReloadTreeGrid: function (treeGridId) {

        $('#' + treeGridId).treegrid('reload');
    },
    TreeGridExpandTo: function (treeGridId, nodeId) {
        $("#" + treeGridId).treegrid("expandTo", nodeId);
    },
    SelectTreeGridRow: function (treeGridId, nodeId) {
        $("#" + treeGridId).treegrid("select", nodeId);
    },
    InitDataGrid: function (dataGridId, isSingle, url, toolbar, defaultSortField, doubleClickRowCallback, clickCallback, idField, loadSuccessCallback, onCheckCallback, onUncheckCallback, onCheckAllCallback, onUncheckAllCallback) {
        $("#" + dataGridId).datagrid({
            pageList: [10, 20, 50, 100],
            fit: true,
            rownumbers: true,
            singleSelect: isSingle,
            pagination: true,
            pageSize: 20,
            url: url,
            method: 'get',
            idField: idField,
            toolbar: '#' + toolbar,
            sortName: defaultSortField,
            sortOrder: 'desc',
            fitColumns: true,
            onDblClickRow: doubleClickRowCallback,
            onClickCell: clickCallback,
            onLoadSuccess: loadSuccessCallback,
            onCheck: onCheckCallback,
            onUncheck: onUncheckCallback,
            onCheckAll: onCheckAllCallback,
            onUncheckAll: onUncheckAllCallback
        });
    },
    InitTreeGrid: function (treeGridId, url, toolbar, idField, treeField, defaultSortField, loadSuccessCallback, doubleClickRowCallback, clickRowCallback,contextMenuCallback) {
        $("#" + treeGridId).treegrid({
            fit: true,
            animate: true,
            rownumbers: true,
            singleSelect: true,
            url: url,
            method: 'get',
            idField: idField,
            treeField: treeField,
            toolbar: '#'+toolbar,
            sortName: defaultSortField,
            sortOrder: 'asc',
            fitColumns: true,
            onLoadSuccess: loadSuccessCallback,
            onDblClickRow: doubleClickRowCallback,
            onClickRow: clickRowCallback,
            onContextMenu: contextMenuCallback
        });
    },
    InitCombotree: function (controlId, url, onSelectCallBack, loadSuccessCallback) {
        $('#' + controlId).combotree({
            url: url,
            method: 'get',
            onSelect: onSelectCallBack,
            onLoadSuccess: loadSuccessCallback
        });
    },
    InitCombobox: function (controlId, url, onSelectCallBack) {
        $('#' + controlId).combobox({
            url: url,
            method: 'get',
            onSelect: onSelectCallBack,
        });
    },
    InitMenu: function(menuId,clickCallBack) {
        $('#'+menuId).menu({
            onClick: clickCallBack
        });
    },
    HandleDataGridSaveResult: function (data, isClose, formId, dialogId, dataGridId, callback) {
        if (data != null && data.State) {
            HandleData.ReloadDataGrid(dataGridId);
            if (isClose) {
                HandleDialog.CloseDialog(dialogId);
                if (callback != undefined) {
                    callback();
                }
            } else {
                HandleData.ClearForm(formId);
                if (callback != undefined) {
                    callback();
                }
            }
        } else {
            HandleTips.Alert(data.Message);
        }
    },
    HandleTreeGridSaveResult: function (data, isClose, formId, dialogId, dataGridId, callback) {
        if (data != null && data.State) {
            HandleData.ReloadTreeGrid(dataGridId);
            if (isClose) {
                HandleData.ClearForm(formId);
                HandleDialog.CloseDialog(dialogId);
                if (callback != undefined) {
                    callback();
                }
            } else {
                HandleData.ClearForm(formId);
                if (callback != undefined) {
                    callback();
                }
                
  
            }
        }
    },
    HandleDialogSaveResult: function (data,dialogId) {
        if (data != null && data.State) {
            HandleDialog.CloseDialog(dialogId);
        }
    },
    HandleExsitResult: function (data, controlId, tips) {
        data = JSON.parse(data);
        if (data != null && data.State === true) {
            $("#" + controlId).val("");
            $.messager.alert('操作提示', tips, 'info');
        }
    },
    HandleDataGridRemoveResult: function (data, dataGridId) {
        data = JSON.parse(data);
        if (data != null && data.State === true) {
            $.messager.alert('操作提示', '删除数据成功!', 'info');
        } else {
            $.messager.alert('操作提示', '删除数据失败!' + data.Message , 'info');
        }
        HandleData.ReloadDataGrid(dataGridId);
    },
    HandleTreeGridRemoveResult: function (data, treeGridId) {
        data = JSON.parse(data);
        if (data != null && data.State === true) {
            $.messager.alert('操作提示', '删除数据成功!', 'info');
        } else {
            $.messager.alert('操作提示', '删除数据失败!', 'info');
        }
        HandleData.ReloadTreeGrid(treeGridId);
    }
    

};
var HandleTips = {
    Alert: function(content) {
        $.messager.alert('操作提示', content, 'info');
    }
};
var HandleRequest = {
    submitRequest: function (url, paras, callback) {
        $.post(url, paras, callback);
    },
    submitForm: function (formId, url, callback, validateCallback) {
        $("#" + formId).form('submit', {
            url: url,
            onSubmit: function () {
                if (validateCallback!=undefined) {
                   return  validateCallback();
                }
                return $(this).form('validate');
            },
            success: callback
        });
    },
    getUrlPara: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var result = window.location.search.substr(1).match(reg);
        if (result != null) {
            return decodeURIComponent(result[2]);
        } else {
            return "undefined";
        }
    }
};

var Validate = {
    IsNotNull: function (value) {
        return value != null;
    },
    IsNotEmpty: function (value) {
        return value !== "";
    },
    IsNotNullOrEmpty: function (value) {
        return value != null && value != "";
    },
    JsonIsNotNull: function (value) {
        return value != null;
    },
    JsonIsNotNullOrEmpty: function (value) {
        return value != null && value.length > 0;
    }
}